/** 定义一个toString函数，用于获取对象的类型 */
const toString = Object.prototype.toString

/**
 * 判断传入的值是否为指定类型
 * @param val 传入的值
 * @param type 指定的类型
 * @returns boolen
 */
export const is = (val: unknown, type: string) =>
  // 调用toString函数，获取传入值的类型，并与指定的类型进行比较
  toString.call(val) === `[object ${type}]`

/**
 * 判断传入的参数val是否为对象
 * @param val 传入的参数
 * @returns
 */
export const isObject = (val: unknown): val is Record<any, any> =>
  // 判断val是否不为null且val的类型是否为Object
  val != null && is(val, 'Object')
export const isString = (val: unknown): val is string => typeof val === 'string'
export const isNumber = (val: unknown): val is number => typeof val === 'number'
export const isBoolean = (val: unknown): val is boolean =>
  typeof val === 'boolean'
export const isRegExp = (val: unknown): val is RegExp => is(val, 'RegExp')
export const isFunction = (val: unknown): val is Function =>
  typeof val === 'function'
export const isArray = (val: any): val is Array<any> =>
  val && Array.isArray(val)
export const isMap = (val: unknown): val is Map<any, any> => is(val, 'Map')
export const isDate = (val: unknown): val is Date => is(val, 'Date')

/**
 * 判断传入的值是否为空
 * @param val 传入的值
 * @returns
 */
export const isEmpty = (val: any) => {
  // 如果传入的值为null或者undefined或者typeof val为undefined，则返回true
  if (val === null || val === undefined || typeof val === 'undefined') {
    return true
  }
  // 如果传入的值为数组或者字符串，则判断其长度是否为0，如果是则返回true
  if (isArray(val) || isString(val)) {
    return val.length === 0
  }
  // 如果传入的值为Map或者Set，则判断其size是否为0，如果是则返回true
  if (val instanceof Map || val instanceof Set) {
    return val.size === 0
  }
  // 如果传入的值为对象，则判断其key的数量是否为0，如果是则返回true
  if (isObject(val)) {
    return Object.keys(val).length === 0
  }
}
export const isEmptyObj = (obj: object) =>
  Reflect.ownKeys(obj).length === 0 && obj.constructor === Object
/**
 * 定义一个泛型函数isDef，用于判断传入的参数是否定义
 * @param val 传入的参数
 * @returns
 */
export const isDef = <T = unknown>(val?: T): val is T =>
  typeof val !== 'undefined'
/**
 * 定义一个泛型函数isUndef，用于判断传入的参数是否未定义
 * @param val 传入的参数
 * @returns
 */
export const isUndef = <T = unknown>(val?: T): val is undefined => !isDef(val)

/**
 * 判断传入的值是否为Window对象
 * @param val 传入的值
 * @returns
 */
export const isWindow = (val: any): val is Window =>
  typeof window !== 'undefined' && is(val, 'Window')

/**
 * 判断传入的值是否为Element对象
 * @param val 传入的值
 * @returns
 */
export const isElement = (val: unknown): val is Element =>
  isObject(val) && !!val.tagName

/**
 * 判断是否为服务器环境
 */
export const isServer = typeof window === 'undefined'
/**
 * 判断是否为客户端环境
 */
export const isClient = !isServer

/**
 * 判断传入的字符串是否为URL
 * @param path 传入的字符串
 * @returns
 */
export const isUrl = (path: string): boolean =>
  // 使用正则表达式判断传入的字符串是否为URL
  /(((^https?:(?:\/\/)?)(?:[-:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%#\/.\w-_]*)?\??(?:[-\+=&%@.\w_]*)#?(?:[\w]*))?)$/.test(
    path
  )

export const isDark = () =>
  window.matchMedia('(prefers-color-scheme: dark)').matches

export const isImgPath = (path: string) =>
  /(https?:\/\/|data:image\/).*?\.(png|jpg|jpeg|gif|svg|webp|ico)/gi.test(path)

export const isEmptyVal = (val: any) =>
  val === '' || val === null || val === undefined
